/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.college;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import jobportal.Dbconn;

/**
 *
 * @author vikash
 */
public class CollegeChangePassword extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd;
        Connection con;
        Statement st;
        ResultSet rs;
        String error = "";
        boolean flag = true;

        String password, oldpass, newpass, clgid, sql;
        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();
            clgid = (String) session.getAttribute("username");
            con = Dbconn.getConn();
            st = con.createStatement();
            oldpass = request.getParameter("oldpass");
            newpass = request.getParameter("newpass");
            rs = st.executeQuery("select * from college where clgid='" + clgid + "' and password='" + oldpass + "'");
            if (rs.next()) {
                sql = "update college set password= ? where clgid = ? ";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setString(1, newpass);
                ps.setString(2, clgid);
                ps.executeUpdate();
                
                 response.sendRedirect("changepass.jsp?status=<strong><font color='green'>password changed successfully</font></strong>");
            } else {
                response.sendRedirect("changepass.jsp?status=<strong><font color='red'>Invalid  password<font></strong>");
            }

        } catch (Exception e) {
            flag = false;
            error += " Exception=" + e;
            out.println("Error=" + error + "\nException::" + e);
            JOptionPane.showMessageDialog(null, "Excepction occurred" + e, "Messging Error:", JOptionPane.WARNING_MESSAGE);
            response.sendRedirect("changepass.jsp?status="+error);

        }
       
    }
}
